<template>
  <div class="app-container">
    <el-card class="mb20">
      <el-form :model="listQ" inline>
        <el-form-item label="流水号">
          <el-input v-model="listQ.serialNo" placeholder="流水号"></el-input>
        </el-form-item>
        <el-form-item label="领料单号">
          <el-input v-model="listQ.issueNo" placeholder="领料单号"></el-input>
        </el-form-item>
        <el-form-item label="物料名称">
          <el-input v-model="listQ.material" placeholder="物料名称"></el-input>
        </el-form-item>
        <el-form-item label="检验人">
          <el-input v-model="listQ.inspector" placeholder="检验人"></el-input>
        </el-form-item>
        <el-form-item label="检验时间">
          <el-date-picker
            v-model="listQ.inspectionAttr"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getList">搜索</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card>
      <el-button type="success" @click="handleAdd">新增</el-button>
      <el-table :data="list">
        <el-table-column
          v-for="item in listC"
          :key="item.prop"
          :label="item.label"
          :prop="item.prop"
        ></el-table-column>
        <el-table-column label="操作" v-slot="{ row }" width="200px">
          <el-button type="primary" @click="handleEdit(row)" size="mini"
            >修改</el-button
          >
          <el-button type="danger" @click="handleDel(row.id)" size="mini"
            >删除</el-button
          >
        </el-table-column>
      </el-table>
      <el-pagination
        style="text-align: right; margin-top: 20px"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pager.page"
        :page-sizes="[2, 10, 20, 30]"
        :page-size="pager.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pager.total"
      >
      </el-pagination>
    </el-card>
    <el-dialog :title="title" center :visible.sync="visible" @close="close">
      <el-form inline ref="listERef" :model="listE">
        <el-form-item label="领料编号" prop="issueNo" required>
          <el-input
            disabled
            v-model="listE.issueNo"
            placeholder="领料编号"
          ></el-input>
        </el-form-item>
        <el-form-item
          ><el-button @click="openIssueDialog" type="primary"
            >选择</el-button
          ></el-form-item
        >
        <el-form-item label="物料名称" prop="materialName" required>
          <el-input
            v-model="listE.materialName"
            placeholder="物料名称"
          ></el-input>
        </el-form-item>
        <el-form-item
          ><el-button @click="openIssueDialog2" type="primary"
            >选择</el-button
          ></el-form-item
        >
        <el-form-item label="抽检数量" prop="samplingQuantity" required>
          <el-input
            v-model="listE.samplingQuantity"
            placeholder="抽检数量"
          ></el-input>
        </el-form-item>
        <el-form-item label="检验值" prop="inspectionValue" required>
          <el-input
            v-model="listE.inspectionValue"
            placeholder="检验值"
          ></el-input>
        </el-form-item>
        <el-form-item label="检验结果" prop="inspectionResult" required>
          <el-input
            v-model="listE.inspectionResult"
            placeholder="检验结果"
          ></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            type="textarea"
            :rows="3"
            resize="none"
            v-model="listE.remark"
            placeholder="备注"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <div>
          <el-button @click="close">取消</el-button>
          <el-button type="primary" @click="submit">确定</el-button>
        </div>
      </template>
    </el-dialog>
    <el-dialog
      title="选择来料单"
      center
      :visible.sync="visibleIssue"
      @close="visibleIssue = false"
    >
      <el-table :data="issueList">
        <el-table-column label="来料编号" prop="issueNo"></el-table-column>
        <el-table-column label="型号" prop="model"></el-table-column>
        <el-table-column label="产品" prop="product"></el-table-column>
        <el-table-column
          label="生产时间"
          prop="productionDate"
        ></el-table-column>
        <el-table-column label="数量" prop="quantity"></el-table-column>
        <el-table-column v-slot="{ row }">
          <el-button type="primary" @click="selectIssue(row)">选择</el-button>
        </el-table-column>
      </el-table>
      <el-divider></el-divider>
      <el-table :data="detailList2">
        <el-table-column
          label="物料名称"
          prop="material.name"
        ></el-table-column>
        <el-table-column label="型号" prop="material.model"></el-table-column>
        <el-table-column
          label="规格"
          prop="material.specification"
        ></el-table-column>
        <el-table-column label="单位" prop="material.unit"></el-table-column>
        <el-table-column v-if="flag" v-slot="{ row }">
          <el-button type="primary" @click="selectMaterial(row)"
            >选择</el-button
          >
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import { delProcessModelingList } from '@/api_zc/mes/process-modeling'
import { getIncomingInspectionList, getissueList, editIncomingInspection, addIncomingInspection, delIncomingInspection } from '@/api_zc/mes/quality-management/incoming-inspection'
export default {
  mounted () {
    this.getList()
    this.getUnit()
  },
  data () {
    return {
      list: [],
      keyword: '',
      listQ: {
        serialNo: '',
        issueNo: '',
        material: '',
        inspector: '',
        inspectionStartTime: "",
        inspectionEndTime: '',
        inspectionAttr: []
      },
      listC: [
        {
          label: '编号',
          prop: 'id'
        },
        {
          label: '流水号',
          prop: 'serialNo'
        },
        {
          label: '领料单号',
          prop: 'issue.issueNo'
        },
        {
          label: '物料名称',
          prop: 'material.name'
        },
        {
          label: '规格',
          prop: 'material.specification'
        },
        {
          label: '型号',
          prop: 'material.model'
        },
        {
          label: '单位',
          prop: 'material.unit'
        },
        {
          label: '需求数量',
          prop: 'requiredQuantity'
        },
        {
          label: '已领数量',
          prop: 'receivedQuantity'
        },
        {
          label: '抽检数量',
          prop: 'samplingQuantity'
        },
        {
          label: '检验项',
          prop: 'inspectionItems'
        },
        {
          label: '标准值上限',
          prop: 'upperLimit'
        },
        {
          label: '标准值下限',
          prop: 'lowerLimit'
        },
        {
          label: '检验值',
          prop: 'inspectionValue'
        },
        {
          label: '检验结果',
          prop: 'inspectionResult'
        },
        {
          label: '检验人',
          prop: 'inspector'
        },
        {
          label: '检验时间',
          prop: 'inspectionTime'
        },
      ],
      listE: {
        id: null,
        issueId: null,
        materialName: null,
        issueNo: null,
        issueDeetailId: null,
        samplingQuantity: null,
        inspectionValue: null,
        inspectionResult: null,
        remark: null
      },
      detailListC: [
        {
          label: '物料',
          prop: 'material'
        },
        {
          label: '型号',
          prop: 'model'
        },
        {
          label: '规格',
          prop: 'specification'
        },
        {
          label: '单位',
          prop: 'unit'
        },
        {
          label: '需求数量',
          prop: 'demandAmount'
        },
        {
          label: '已用数量',
          prop: 'usedAmount'
        },
        {
          label: '剩余数',
          prop: 'remainAmount'
        },
        {
          label: '备注',
          prop: 'remark'
        },
      ],
      pager: {
        limit: 10,
        page: 1,
        total: 0
      },
      visible: false,
      title: '',
      isAdd: false,
      issueList: [],
      visibleIssue: false,
      detailList2: [],
      flag: false
    }
  },
  methods: {
    async getUnit () {
      const { data: { content } } = await getissueList()
      this.issueList = content
    },
    async getList () {
      const { data: { content, total } } = await getIncomingInspectionList({ ...this.listQ, keyword: this.keyword, page: this.pager.page, limit: this.pager.limit })
      this.list = content
      this.pager.total = total
    },
    handleDisabled (flag) {
      if (this.isAdd) {
        return false
      }
      if (this.listE.publishStatus == '已发布' || (this.listE.publishStatus == '未发布' && flag)) {
        return true
      }
      return false
    },
    handleReset () {
      Object.assign(this.listQ, this.$options.data().listQ)
      this.getList()
    },
    handleSizeChange (val) {
      this.pager.limit = val
      this.getList()
    },
    handleCurrentChange (val) {
      this.pager.page = val
      this.getList()
    },
    close () {
      this.visible = false
    },
    handleEdit (row) {
      this.visible = true
      this.title = "修改来料检验"
      this.isAdd = false
      this.$nextTick(() => {
        Object.assign(this.listE, {
          id: row.id,
          samplingQuantity: row.samplingQuantity,
          inspectionValue: row.inspectionValue,
          inspectionResult: row.inspectionResult,
          remark: row.remark,
          materialName: row.material.name,
          issueNo: row.issue.issueNo,
          issueId: row.issue.id,
          issueDeetailId: null,
        })
      })
    },
    handleAdd () {
      this.visible = true
      this.title = "修改来料检验"
      this.isAdd = true
      this.$nextTick(() => {
        this.$refs.listERef.resetFields()
      })
    },
    async handleDel (id) {
      await delProcessModelingList({ ids: [id] })
      this.$message.success('删除成功')
      this.getList()
    },
    submit () {
      this.$refs.listERef.validate(async (callback) => {
        if (!callback) {
          return this.$message.error('请填写必要字段')
        }
        if (this.isAdd) {
          await addIncomingInspection(this.listE)
          this.$message.success('添加成功')
        } else {
          await editIncomingInspection(this.listE)
          this.$message.success('修改成功')
        }
        this.visible = false
        this.getList()
      })
    },
    openIssueDialog () {
      this.flag = false
      this.visibleIssue = true
    },
    selectIssue (row) {
      this.listE.issueId = row.id
      this.listE.issueNo = row.issueNo
      this.detailList2 = row.detailList
    },
    selectMaterial (row) {
      this.listE.issueDeetailId = row.id
      this.visibleIssue = false
    },
    openIssueDialog2 () {
      this.visibleIssue = true
      this.flag = true
    }
  }
}
</script>

<style lang="scss">
</style>